package com.fzy.core.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IOUtils extends org.apache.commons.io.IOUtils {
	private static final Logger log = LoggerFactory.getLogger(IOUtils.class);
	
	public static void write(String content, String fileName, String path) {
		write(content, fileName, path, "UTF-8");
	}
	
	public static void write(String content, String fileName, String path, String charset) {
		File f = new File(path);
		if (!f.exists())
			f.mkdirs();
		OutputStreamWriter fw = null;
		try {
			fw = new OutputStreamWriter(new FileOutputStream(new File(path,
					fileName)), charset);
			fw.write(content);
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		} finally {
			if (fw != null) {
				try {
					fw.flush();
					fw.close();
				} catch (IOException e) {
					log.error(e.getMessage(), e);
				}
			}
		}
	}
	
	public static boolean io(InputStream in, OutputStream out) {
		boolean b = true;
		byte[] buffer = new byte[1024 * 4];
		try {
			int readLength;
			while ((readLength = in.read(buffer)) > -1) {
				out.write(buffer, 0, readLength);
			}
			out.flush();
			out.close();
			in.close();
		} catch (IOException e) {
			b = false;
			log.error("io IOException errorMessage :" + e.getMessage());
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e1) {
				log.error("io finally IOException  errorMessage :"
						+ e1.getMessage());
			}
		}
		return b;
	}
}
